/**
 * @purpose 基础领域对象
 * @author daizs
 * @created Feb 9, 2010
 */
package platform.common.bo;

import java.io.Serializable;
import java.util.UUID;

import platform.common.bo.IIdentifiableObj;
import platform.common.bo.IdentifiableObj;
import platform.common.bo.LogabledObj;


public class IdentifiableObj extends LogabledObj implements Serializable, IIdentifiableObj{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Serializable id;

	/* (non-Javadoc)
	 * @see platform.common.bo.IIdentifiableObj#getId()
	 */
	public Serializable getId() {
		return id;
	}

	/* (non-Javadoc)
	 * @see platform.common.bo.IIdentifiableObj#setId(java.io.Serializable)
	 */
	public void setId(Serializable id) {
		this.id = id;
	}

	/* (non-Javadoc)
	 * @see platform.common.bo.IIdentifiableObj#generatorUUID()
	 */
	public void generatorUUID() {
		UUID uuid = UUID.randomUUID();
		this.id = uuid.toString().replaceAll("-", "");
	}

	/* (non-Javadoc)
	 * @see platform.common.bo.IIdentifiableObj#setIdAsNew()
	 */
	public void setIdAsNew() {
		this.id = null;
	}

	/* (non-Javadoc)
	 * @see platform.common.bo.IIdentifiableObj#isNew()
	 */
	public boolean isNew() {
		boolean bNew=false;
		if(id == null||id.toString().trim().equals("")){
			bNew=true;
		}
		return bNew;
	}



	public boolean equals(Object o) {
		return this.compareTo(o) == 0;
	}

	public int hashCode() {
		int result = 0;
		if (id==null)
			result = System.identityHashCode(this);
		else
			result = (id + "").hashCode();

		return result;
	}

	public int compareTo(Object o) {
		if (this == o)
			return 0;
		if (o == null || this.getId() == null)
			return -1;

		else {
			if (this.getId() != null && this.getId() instanceof Integer
					&& o instanceof IdentifiableObj) {
				Integer integer = (Integer) (this.getId());
				IIdentifiableObj identifiable = ((IIdentifiableObj) o);
				Integer anotherInteger = (Integer) identifiable.getId();
				return integer.compareTo(anotherInteger);

			} else if (this.getId() != null && this.getId() instanceof Long
					&& o instanceof IdentifiableObj)
				return ((Long) (this.getId())).compareTo((Long) ((IIdentifiableObj) o)
						.getId());
			else if (o instanceof IdentifiableObj)
				return this.getId().toString().compareTo(
						((IIdentifiableObj) o).getId().toString());
			else
				return -1;
		}
	}



}
